<?php
namespace Admin\Controller;

use Admin\Model\Admin;
use Common\Model\Movie;
use Think\Controller;

/**
 * 电影管理
 * @author  Zou Yiliang
 * @since   1.0
 */
header("Content-type: text/html; charset=utf-8");

class CinemaController extends BaseController
{
    public function index()
    {
         $mh=$_GET['name'];
         $key=$_GET['key'];
         $condition["$key"]=array('like',"%".$mh."%");
         $count=M('user')->where($condition)->count();
         $Page= new \Think\Page($count,6);
         $Page->setConfig('next','下一页');
         $Page->setConfig('prev','上一页');
        //分页查询电影数据
         $limit=$Page->firstRow.','.$Page->listRows;
         $show= $Page->show();
         $list=D('User')->getAll($condition,$limit);
        if(empty($list)){
            $this->assign('result','没有此搜索项');
            $this->display();
        }else{
            $this->assign('result','共查出'.$count.'条数据');
            $this->assign('list', $list);
            $this->assign('page', $show);
            $this->display();
        }
    }

    public function create()
    {
        if(empty($_POST)){
            $this->display();
        }else{
            $model1=M('user')->where(['username'=>$_POST['name']])->select();
            if(!empty($model1)){
                echo '商户已存在';
                exit;
            }
              $model=D('user');
              $data=$model->create();
            if(!$data){
                echo $model->getError();
                exit;
            }else{
            $data['username']=$_POST['name'];
                $data['password_salt']=rand(1,9999);
                $data['password_hash']=md5($_POST['pwd'].$data['password_salt']);
            $data['name']=$_POST['cinema'];
            $data['address']=$_POST['address'];
            $data['created_at']=time();
            $results=D('user')->setAll($data);
            if($results){
                echo '添加成功'."<a href=".U('Cinema/index')." style='margin-left:880px' class=\"btn btn-default\">返回</a>";
            }else{
                echo '添加失败';
              }
            }
        }
    }

    public function update()
    {
        if(empty($_POST)){
            $model = M('user')->where(array('id' => $_GET['id']))->find();
            if (empty($model)) {
                throw new \Exception("id不存在");
            }
            $this->assign('model', $model);
            $this->display();
        }else{
            if(empty($_POST['name'])){
                echo '用户名不能为空';
                return false;
            }
            if(empty($_POST['cinema'])){
                echo '影院名称不能为空';
                return false;
            }
            if(empty($_POST['address'])){
                echo '地址不能为空';
                return false;
            }
            $data['username']=$_POST['name'];
            $data['name']=$_POST['cinema'];
            $data['address']=$_POST['address'];
            $data['updated_at']=time();
            $model2=M('user')->where(['id'=>$_POST['id']])->save($data);
            if($model2){
                echo '修改成功';
            }
        }
    }

    public function delete(){
        $model=M('user')->delete($_GET['id']);
        $this->redirect('Cinema/index');
    }
}